package com.uroboros.spring.guides.accessing.data.jpa;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@Slf4j
@SpringBootApplication
public class SpringGuidesAccessingDataJpaApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringGuidesAccessingDataJpaApplication.class, args);
	}

	@Bean
	public CommandLineRunner init(CustomerRepository repository) {
		return args -> {
			repository.save(new Customer("Jack", "Bauer"));
			repository.save(new Customer("Chloe", "O'Brian"));
			repository.save(new Customer("Kim", "Bauer"));
			repository.save(new Customer("David", "Palmer"));
			repository.save(new Customer("Michelle", "Kessler"));

			// fetch all customers
			log.info("Customers found with findAll():");
			log.info("-------------------------------");
			repository.findAll().forEach(customer -> log.info(customer.toString()));
			log.info("");

			// fetch an individual customer by ID
			Customer customer = repository.findCustomerById(1L);
			log.info("Customer found with findCustomerById(1L):");
			log.info("--------------------------------");
			log.info(customer.toString());
			log.info("");

			// fetch customers by last name
			log.info("Customer found with findCustomerByLastName('Bauer'):");
			log.info("--------------------------------------------");
			repository.findCustomerByLastName("Bauer").forEach(bauer -> log.info(bauer.toString()));
			log.info("");
		};
	}

}
